import os
import xlrd


# 创建文件夹，若文件夹存在则不创建
def makedirs(folderName):
    try:
        os.makedirs(folderName)  # 创建文件夹
    except FileExistsError:
        pass


def loadExcel(fileName):
    try:
        # print('正在读取Excel文件...')
        excel = xlrd.open_workbook(fileName)        # 打开Excel文件
        # print('文件读取完毕！')
    except FileNotFoundError:
        print('文件名不存在！')
    else:
        return excel


def read_sheetdata(excel, sheetName):
    try:
        # print('正在读取工作表...')
        table = excel.sheet_by_name(sheetName)  # 打开工作表
        # print('工作表读取完毕！')
    except xlrd.biffh.XLRDError as e:
        print('文件"{}"不存在工作表"{}"'.format(fileName, sheetName))
        print('Reason:', e)
    else:
        # print('正在读取工作表数据...')
        cols_number = table.ncols  # 获取工作表列数，由于数据计算原因，读取的数据与Excel属于转置关系，方便计算

        table_value = [[]] * cols_number  # 设置空列表用于储存工作表数据
        for i in range(cols_number):
            table_value[i] = table.col_values(i)
        # print('数据读取完毕！')
        return table_value


# 读取Excel数据中工作表全部数据
def readExcel(fileName, sheetName):
    excel = loadExcel(fileName)
    table_value = read_sheetdata(excel, sheetName)
    return table_value

# 写入Excel数据
def writeExcel():
    pass


if __name__ == '__main__':
    fileName = 'boundary data/WeatherData.xlsx'
    sheetName = '逐时气象参数'
    value = readExcel(fileName, sheetName)
    print(value)
